﻿//Format Generic Mask 
function FormatField(campo, Mascara, evento) {
    var boleanoMascara;
    var Digitato = (window.event) ? event.keyCode : evento.which;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace(exp, "");

    var posicaoCampo = 0;
    var NovoValorCampo = "";
    var TamanhoMascara = campoSoNumeros.length;

    if (Digitato != 8) { // backspace 
        for (i = 0; i <= TamanhoMascara; i++) {
            boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                                                || (Mascara.charAt(i) == "/"))
            boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(")
                                                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                TamanhoMascara++;
            } else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
            }
        }
        campo.value = NovoValorCampo;
        return true;
    } else {
        return true;
    }
}
//Mask CPF
function MaskCPF(cpf) {
    if (OnlyNumbers(cpf) == false) {
        event.returnValue = false;
    }
    return FormatField(cpf, '000.000.000-00', event);
}
//Validate CPF
function ValidateCPF(Objcpf) {
    var cpf = Objcpf.value;
    exp = /\.|\-/g
    cpf = cpf.toString().replace(exp, "");
    var digitoDigitado = eval(cpf.charAt(9) + cpf.charAt(10));
    var soma1 = 0, soma2 = 0;
    var vlr = 11;

    for (i = 0; i < 9; i++) {
        soma1 += eval(cpf.charAt(i) * (vlr - 1));
        soma2 += eval(cpf.charAt(i) * vlr);
        vlr--;
    }
    soma1 = (((soma1 * 10) % 11) == 10 ? 0 : ((soma1 * 10) % 11));
    soma2 = (((soma2 + (2 * soma1)) * 10) % 11);

    var digitoGerado = (soma1 * 10) + soma2;
    if (digitoGerado != digitoDigitado) {
        alert('CPF Inválido!');
        Objcpf.value = '';
    }
}
//Mask Date
function MaskDate(data) {
    if (OnlyNumbers(data) == false) {
        event.returnValue = false;
    }
    return FormatField(data, '00/00/0000', event);
}
//Validate Date
function ValidateDate(data) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
    if (!data.value.match(expReg)) {
        alert('Data Inválida!');
        data.value = '';
    }
}

//Mask CEP
function MaskCEP(cep) {
    if (OnlyNumbers(cep) == false) {
        event.returnValue = false;
    }
    return FormatField(cep, '00000-000', event);
}
//Validate CEP
function ValidateCEP(cep) {
    exp = /\d{2}\.\d{3}\-\d{3}/
    if (!exp.test(cep.value)) {
        alert('Cep Inválido!');
        cep.value = '';
    }
}
//Validate E-Mail
function ValidateEmail(email) {
    var txt = email.value;
    if ((txt.length != 0) && ((txt.indexOf("@") < 1) || (txt.indexOf('.') < 7))) {
        alert('Email inválido!');
        email.value = '';
    }
}
//Validate Phone
function ValidatePhone(e) {
    var key = (window.event) ? event.keyCode : e.which;
    if (OnlyNumbers(e) || key == 45 || key == 40 || key == 41 || key == 32) 
    {
        return true;
    }
    else {
        return false;
    }
}
//Validate Numbers Only
function OnlyNumbers(e) {
    var key = (window.event) ? event.keyCode : e.which;
    if ((key > 47 && key < 58)) return true;
    else {
        if (key == 8 || key == 0) return true;
        else return false;
    }
}